//
//  Pet_RechargeViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_RechargeViewController: BasicViewController {
    
    let backV = UIImageView.initImgView(img: "topV")
    let rechrgeB = UIButton.initV(img: "")
    let rechrgeL = UILabel.initV(UIFont.setTypeFace(11,type: .bold), text: "My coin", textColor: mainColor)
    let coinsL = UILabel.initV(UIFont.setTypeFace(25,type: .bold), text: "0", textColor: mainColor)
    let recL = UILabel.initV(UIFont.setTypeFace(19,type: .extraBold), text: "Recharge", textColor: .black)

    private lazy var table: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.backgroundColor = .clear
        view.delegate = self
        view.dataSource = self
        view.showsVerticalScrollIndicator = false
        view.register(RechargeTableViewCell.self, forCellReuseIdentifier: "cell")
        view.rowHeight = 90.Fit
        return view
    }()
    let coinArr = ["20","25","20","40","80","90","165","260","450","600","760"]
    let priceArr = ["$0.99","$1.99","$2.99","$6.99","$9.99","$14.99","$19.99","$34.99","$59.99","$79.99","$94.99"]
    let productArr = NSMutableArray(array: ["com_bobo_0.99_20coins",
                                          "com_bobo_1.99_25coins",
                                          "com_bobo_2.99_20coins_off3",
                                          "com_bobo_6.99_40coins",
                                          "com_bobo_9.99_80coins",
                                          "com_bobo_14.99_90coins",
                                          "com_bobo_19.99_165coins",
                                          "com_bobo_34.99_260coins",
                                          "com_bobo_59.99_450coins",
                                          "com_bobo_79.99_600coins",
                                          "com_bobo_94.99_760coins",])

//    let productArr = NSMutableArray(array: ["6aa6109799a791f97d2da1c906883539",
//                                          "fde384c78e518fcfae631f263ca1075b",
//                                          "342927ec03ee29aff37e61b674f9f8fe",
//                                          "a3bac1608dfcb0ace9bc8097c1273473",
//                                          "b66710911f4ae1a1e13f7afb936b70b0",
//                                          "290318ed821a6d7bfc195028aa51d226",
//                                          "583dc1e02e56fe1bdeea26d31033c726",
//                                          "bd7999df4e2ed3c476120eb3178d17f6",
//                                          "8398a1ebff5b7a8fb3d79c04d3cc141e",
//                                          "e45ae96a16c6603abb5ddef5be0dacc8",
//                                          "838473b7bf954dca42cf13f56fff1d8f",])
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = ""
        view.subviews {
            backV.subviews {
                rechrgeB.subviews {
                    rechrgeL
                    coinsL
                    recL
                }
            }
            table
        }
        
        backV.left(0).right(0).top(0).height(250.Fit)
        table.left(0).right(0).bottom(0.Fit)
        table.Top == backV.Bottom + 30.Fit
        
        rechrgeB.left(15.Fit).right(15.Fit).height(88.Fit).bottom(15.Fit)
        rechrgeB.setBackgroundImage(.init(named: "rechargeB"), for: .normal)
        
        coinsL.height(25.Fit).width(100.Fit).left(25.Fit).top(22.Fit)
        rechrgeL.left(20.Fit).height(25.Fit).width(80)
        rechrgeL.Top == coinsL.Bottom
        
        recL.backgroundColor = .color(hexString: "#E5F789")
        recL.right(25.Fit).height(40.Fit).width(113.Fit)
        recL.setAllCorners(corner: 14.Fit)
        recL.centerVertically()
        recL.textAlignment = .center

        NotificationCenter.default.addObserver(forName: rechargeNotice, object: nil, queue: .main) { notice in
            self.coinsL.text = USEINFOMODEL.coins ?? "0"
        }
    }

}

extension Pet_RechargeViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return coinArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RechargeTableViewCell
        cell.selectionStyle = .none
        cell.coinsL.text = coinArr[indexPath.row]
        cell.priceL.text = priceArr[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        IAPManager.shared.coinString = coinArr[indexPath.row]
        IAPManager.shared.startPaymentWithProductId(productId: productArr[indexPath.row] as! String)
    }
    
}


class RechargeTableViewCell: UITableViewCell {
    
    let backV = UIView.init(frame: .zero)
    let coinsL = UILabel.initV(.setTypeFace(20,type: .bold), text: "", textColor:mainColor)
    let priceL = UILabel.initV(.setTypeFace(15,type: .bold), text: "", textColor: .black)
    let iconI = UIImageView.initImgView(img: "coinsicon")
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.subviews {
            backV.subviews {
                iconI
                coinsL
                priceL
            }
        }
        
        backV.left(15.Fit).right(15.Fit).top(15.Fit).height(70.Fit)
        backV.setAllCorners(corner: 35.Fit)
        backV.backgroundColor = .init(white: 1, alpha: 0.15)
        backV.setAllCorners(corner: 25.Fit)
        
        iconI.width(40.Fit).left(15.Fit).height(46.Fit)
        iconI.centerVertically()
        
        priceL.width(90.Fit).height(40.Fit).right(15.Fit)
        priceL.setAllCorners(corner: 10.Fit)
        priceL.centerVertically()
        priceL.backgroundColor = mainColor
        priceL.textAlignment = .center
        
        coinsL.height(30.Fit).width(100.Fit)
        coinsL.Leading == iconI.Trailing + 15.Fit
        coinsL.centerVertically()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
